// pages/goods_detail/goods_detail.

import { request } from '../../request/index.js';
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsObj: [],
  },

  // 全局商品信息
  goodsInfo:[],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { goods_id } = options;
    this.getGoodDetail(goods_id);
  },

  // 获取数据
  async getGoodDetail(goods_id) {
    const goodsObj = await request({url: '/goods/detail',data: { goods_id },});
    this.goodsInfo = goodsObj;
    this.setData({
      goodsObj: {
        goods_name: goodsObj.goods_name,
        goods_price: goodsObj.goods_price,
        goods_introduce: goodsObj.goods_introduce.replace(/\.webp/g, '.jpg'),
        pics: goodsObj.pics,
      },
    });
  },

  handlePreviewImage(e) {
    // 构造需要预览的图片数组
    const urls = this.goodsInfo.pics.map(v=>v.pics_mid)
    // 接受传过来的参数
    const current = e.currentTarget.dataset.url;
    wx.previewImage({
      current,
      urls,
    });
  }
});
